package com.atguigu.flink.chapter05.practice;

import com.atguigu.flink.bean.UserBehavior;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @Author lzc
 * @Date 2023/6/19 14:55
 */
public class Pv_1 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
    
        env
            .readTextFile("input/UserBehavior.csv")
            .map(new MapFunction<String, UserBehavior>() {
                @Override
                public UserBehavior map(String value) throws Exception {
                    String[] data = value.split(",");
                    return new UserBehavior(
                        Long.valueOf(data[0]),
                        Long.valueOf(data[1]),
                        Long.valueOf(data[2]),
                        data[3],
                        Long.valueOf(data[4]),
                        1L
                    );
                }
            
            })
            .filter(ub -> "pv".equals(ub.getBehavior()))
            .keyBy(UserBehavior::getBehavior)
            //.sum("pvCount")
            .reduce(new ReduceFunction<UserBehavior>() {
                @Override
                public UserBehavior reduce(UserBehavior value1, UserBehavior value2) throws Exception {
                    value1.setPvCount(value1.getPvCount() + value2.getPvCount());
                    
                    return value1;
                }
            })
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
/*
pv: page view
 页面访问量
 
 

计算 pv 值?

 */